package com.jnu.HomeworkApplication;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager2 viewPager2 = findViewById(R.id.viewBookPage2);
        viewPager2.setAdapter(new BookFragmentStateAdapter(this));
        TabLayout tabLayout = findViewById(R.id.TabLayout_BookHeader);
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("图书");
                    break;
                case 1:
                    tab.setText("新闻");
                    break;
                case 2:
                    tab.setText("卖家");
                    break;
                case 3:
                    tab.setText("游戏");
                    break;
            }
        });
        tabLayoutMediator.attach();
    }

    private static class BookFragmentStateAdapter extends FragmentStateAdapter {
        public BookFragmentStateAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch (position) {
                case 0:
                    return BookFragment.newInstance();
                case 1:
                    return NewsFragment.newInstance();
                case 2:
                    return SellerFragment.newInstance();
                default:
                    return GameFragment.newInstance();
            }
        }

        @Override
        public int getItemCount() {
            return 4;
        }
    }
}